﻿using System;
using System.Collections.Generic;
using System.IO;
using System.Runtime.Serialization.Formatters.Binary;
using Mx.Ipn.Escom.TT20080015.Modelo.Codificacion;
using Mx.Ipn.Escom.TT20080015.Modelo.Datos;
using Mx.Ipn.Escom.TT20080015.Modelo.Memoria;

namespace Mx.Ipn.Escom.TT20080015.Vistas.Memoria
{
    public partial class EntrenarBaseConocimientoView : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (Session["user"] == null)
                Server.Transfer("~/Default.aspx", false);
            else if (!((Usuario)Session["user"]).Perfil.Equals("Administrador"))
                Server.Transfer("../../vistas/control/Control.aspx", false);
        }

        protected void btnEntrenar_Click(object sender, EventArgs e)
        {
            //No va asi
            List<Asociacion> vectors = new List<Asociacion>();
            Parametro p = new Parametro();
            BAMAlphaBeta m = new BAMAlphaBeta();
            try
            {
                StreamReader sr = File.OpenText("C:\\breast-cancer.data");
                string s = "";
                while ((s = sr.ReadLine()) != null)
                {
                    vectors.Add(p.obtenerTrama(s.Split(',')));
                }
                sr.Close();
            }
            catch
            {
                
            }
            m.Learn(vectors);
            try
            {
                BinaryFormatter bformatter = new BinaryFormatter();
                Stream s = File.Open("c:\\test.kb", FileMode.OpenOrCreate);
                bformatter.Serialize(s, m);
                s.Close();
            }
            catch (Exception)
            {
                
            }
            Context.Items.Add("msg", "Se ha terminado de entrenar la base de conocimiento satisfactoriamente");
            Server.Transfer("../../vistas/control/Control.aspx", false);
        }
    }
}
